
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for group_privacy_settings.
	''' </summary>
	Public Partial Class group_privacy_settings
		Inherits BaseWebPage
		Protected arrMemberGroups As ArrayList = Nothing
		Protected nGroupsCount As Double = 0
		Protected usr As User = Nothing
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Put user code to initialize the page here

			usr = LoggedUser

			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			Me.initPageTitles(Constants.GROUP_PRIVACY_SETTINGS, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			If Constants.SHOW_GROUPS <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			' init help link
			hlHelp.NavigateUrl = "../#public"
			hlHelp.Attributes.Add("onclick", "popWinHelp('" + Constants.HELP_GROUPS + "?" + Constants.GROUP_HELP_PAGE_PARAM + "=" + Constants.GROUP_HELP_PAGE_PRIVACY_SETTINGS_VALUE + "')")

			arrMemberGroups = Groups.MyMemberGroups(usr.ID)
			nGroupsCount = arrMemberGroups.Count

			If nGroupsCount <= 0 Then
				tblMemberGroups.Visible = False
				lblMessage.Visible = True
				lblMessage.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgNotMemberships_TEXT")
			Else
				dgPager.RedirectPage = Constants.GROUP_PRIVACY_SETTINGS
				dgPager.MaxPageNumber = Convert.ToInt32(Math.Ceiling(nGroupsCount / Constants.MAX_MEMBER_GROUPS_ON_PAGE))
				dgPager.Action = "-1"
				dgPagerUp.RedirectPage = Constants.GROUP_PRIVACY_SETTINGS
				dgPagerUp.MaxPageNumber = Convert.ToInt32(Math.Ceiling(nGroupsCount / Constants.MAX_MEMBER_GROUPS_ON_PAGE))
				dgPagerUp.Action = "-1"

				' initialize listing literals
				dgPager.dgListing.TotalCount = Convert.ToInt32(nGroupsCount)
				dgPagerUp.dgListing.TotalCount = Convert.ToInt32(nGroupsCount)
				'initialize current page number
				If Convert.ToInt32(Request.Params("page")) = 0 OrElse dgPager.CurrentPageNumber = 1 Then
					dgPager.CurrentPageNumber = 1
					dgPagerUp.CurrentPageNumber = 1
					ViewState("StartIndex") = 0
				Else
					dgPager.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					dgPagerUp.CurrentPageNumber = Convert.ToInt32(Request.Params("page"))
					ViewState("StartIndex") = (dgPager.CurrentPageNumber - 1) * Constants.MAX_MEMBER_GROUPS_ON_PAGE
				End If

				dgPager.PagesRest = dgPager.MaxPageNumber - dgPager.FirstPageNumber + 1
				dgPager.LastPageNumber = Math.Min(dgPager.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)
				dgPagerUp.PagesRest = dgPagerUp.MaxPageNumber - dgPagerUp.FirstPageNumber + 1
				dgPagerUp.LastPageNumber = Math.Min(dgPagerUp.PagesRest, Constants.MAX_PAGES_NUMS_ON_PAGE)

				If Not IsPostBack Then
					dgMemberGroups.VirtualItemCount = Convert.ToInt32(nGroupsCount)
					BindGrid()
				End If
			End If
		End Sub

		Protected Function CreateDataSource() As ICollection

			Dim dt As New DataTable()
			Dim dr As DataRow

			Dim startIndex As Integer = Convert.ToInt32(ViewState("StartIndex"))
			Dim endIndex As Integer = startIndex + Constants.MAX_MEMBER_GROUPS_ON_PAGE
			Dim t As Integer = 0
			Dim counter As Integer = 0

			endIndex = Math.Min(endIndex, Convert.ToInt32(nGroupsCount))
			t = (endIndex - startIndex)
			dgPager.dgListing.StartIndex = startIndex + 1
			dgPager.dgListing.EndIndex = endIndex
			dgPagerUp.dgListing.StartIndex = startIndex + 1
			dgPagerUp.dgListing.EndIndex = endIndex

			dt.Columns.Add(New DataColumn("GroupID", GetType(Integer)))
			dt.Columns.Add(New DataColumn("GroupImageURL", GetType(String)))
			dt.Columns.Add(New DataColumn("GroupURL", GetType(String)))
			dt.Columns.Add(New DataColumn("GroupName", GetType(String)))
			dt.Columns.Add(New DataColumn("AcceptMemberCommentsYesChecked", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("AcceptMemberCommentsNoChecked", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("AcceptModeratorCommentsYesChecked", GetType(Boolean)))
			dt.Columns.Add(New DataColumn("AcceptModeratorCommentsNoChecked", GetType(Boolean)))

			Dim pic As GroupPicture
			Dim gm As GroupMember
			' List user member's groups
			' NOTE: The groups to which the user is moderator are not included				
			For Each grp As Group In arrMemberGroups
				If counter >= startIndex AndAlso counter < endIndex Then
					dr = dt.NewRow()

					pic = grp.DefaultPicture
					gm = grp.GetUserMembership(usr.ID)

					dr(0) = grp.ID
					If pic <> Nothing Then
						dr(1) = Pictures.GetPictureSrcFullURL(pic, True, "../")
					Else
						dr(1) = Constants.DEFAULT_THUMBNAIL_IMAGE_URL
					End If
					dr(2) = "../" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID
					dr(3) = grp.Name
					dr(4) = (gm.AcceptMemberBulletin = True)
					dr(5) = (gm.AcceptMemberBulletin = False)
					dr(6) = (gm.AcceptModeratorBulletin = True)
					dr(7) = (gm.AcceptModeratorBulletin = False)

					dt.Rows.Add(dr)
				End If
				System.Math.Max(System.Threading.Interlocked.Increment(counter),counter - 1)
			Next

			Dim dv As New DataView(dt)
			Return dv
		End Function

		Protected Sub BindGrid()
			'init datagrid with database data ordered by "orederBy" param
			dgMemberGroups.DataSource = CreateDataSource()
			dgMemberGroups.DataBind()
		End Sub


		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()

		End Sub
#End Region

		Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Dim hdnGrpID As System.Web.UI.HtmlControls.HtmlInputHidden
			Dim rbAccMemYes As System.Web.UI.WebControls.RadioButton
			Dim rbAccModYes As System.Web.UI.WebControls.RadioButton
			Dim gm As GroupMember = Nothing
			Dim grp As Group = Nothing
			Dim curGrpID As Integer = -1
			Dim nAccMemValue As Integer = -1
			Dim nAccModValue As Integer = -1

			For Each dgi As DataGridItem In Me.dgMemberGroups.Items
				hdnGrpID = DirectCast(dgi.Cells(0).Controls(1), HtmlInputHidden)
				rbAccMemYes = DirectCast(dgi.Cells(1).Controls(1), RadioButton)
				rbAccModYes = DirectCast(dgi.Cells(1).Controls(5), RadioButton)

				curGrpID = Convert.ToInt32(hdnGrpID.Value)
				nAccMemValue = Convert.ToInt32(Request.Params(rbAccMemYes.NamingContainer.UniqueID + ":" + rbAccMemYes.GroupName))
				nAccModValue = Convert.ToInt32(Request.Params(rbAccModYes.NamingContainer.UniqueID + ":" + rbAccModYes.GroupName))
				grp = Groups.GetGroupByID(curGrpID)

				gm = grp.GetUserMembership(usr.ID)
				gm.AcceptMemberBulletin = (nAccMemValue = 1)
				gm.AcceptModeratorBulletin = (nAccModValue = 1)
				gm.Save()
			Next
			lblMessage.Visible = True
		End Sub

		Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Response.Redirect(Constants.ACCOUNT_SETTINGS_PAGE)
		End Sub

		====================
		' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
		Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
			Get
				Return Migrated_Form1
			End Get
		End Property
		'set { Migrated_Form1 = value; }
	End Class
End Namespace






